package com.nvelickovic10.snake.beans.snake;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;

import com.nvelickovic10.snake.main.AppCore;

public class SnakeBits implements Serializable {

	private static final long serialVersionUID = -3759345730056710818L;
	private Point position;
	private boolean isHead;
	private Direction dirrection;

	public SnakeBits(Point position) {
		setPosition(position);
		setHead(false);
	}

	public Point getPosition() {
		return position;
	}

	public void setPosition(Point position) {
		this.position = position;
	}

	private int getX() {
		return (int) position.getX();
	}

	private int getY() {
		return (int) position.getY();
	}

	public void paintBit(Graphics2D g) {
		BufferedImage img = null;
		String path = "png/body.png";
		if (isHead) {
			switch (dirrection) {
			case UP:
				path = "png/head_UP.png";
				break;
			case DOWN:
				path = "png/head_DOWN.png";
				break;
			case LEFT:
				path = "png/head_LEFT.png";
				break;
			case RIGHT:
				path = "png/head_RIGHT.png";
				break;
			default:
				break;
			}
		}

		try {
			img = ImageIO.read(new File(path));
		} catch (IOException e) {
			e.printStackTrace();
		}
		g.drawImage(img, null, AppCore.getInstance().getFactor() * (getX() - 1)
				+ AppCore.getInstance().getLeft() + 1, AppCore.getInstance()
				.getFactor()
				* (getY() - 1)
				+ AppCore.getInstance().getTop()
				+ 1);

	}

	public boolean isHead() {
		return isHead;
	}

	public void setHead(boolean isHead) {
		this.isHead = isHead;
		setDirrection(Direction.UP);
	}

	public Direction getDirrection() {
		return dirrection;
	}

	public boolean setDirrection(Direction dirrection) {
		if ((isHead
				&& dirrection != Direction
						.getOppositeDirection(this.dirrection) && dirrection != this.dirrection)
				|| !isHead) {
			this.dirrection = dirrection;
			return true;
		}
		return false;

	}
}
